﻿using ET_DbCommon;
using ET_DbCommon.DAL;
using ET_DbCommon.Model;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ET.ScoreTeacherClient.Views
{
    /// <summary>
    /// UMLExport.xaml 的交互逻辑
    /// </summary>
    public partial class UMLExport : UserControl
    {
        public ObservableCollection<UMLQuestionViewModel> QuestionList { get; set; } = new ObservableCollection<UMLQuestionViewModel>();
        public UMLExport()
        {
            InitializeComponent();
        }
        bool sign = true;
        public class UMLQuestionViewModel:NotifyPropertyChanged
        {
            private int m_TestQuestionID;
            public int TestQuestionID
            {
                get { return m_TestQuestionID; }
                set
                {
                    this.m_TestQuestionID = value;
                    this.OnPropertyChanged();
                }
            }
            private string m_QuestionName;
            public string QuestionName
            {
                get { return m_QuestionName; }
                set
                {
                    this.m_QuestionName = value;
                    this.OnPropertyChanged();
                }
            }
            private bool m_IsChecked;
            public bool IsChecked
            {
                get { return m_IsChecked; }
                set
                {
                    this.m_IsChecked = value;
                    this.OnPropertyChanged();
                }
            }
            public UMLQuestionViewModel(UMLQuestionModel model)
            {
                this.TestQuestionID = model.TestQuestionID;
                this.QuestionName = model.QuestionName;
            }
        }
        private void scrollbar_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (scrollbar == null)
                return;
            double num = Math.Abs((int)(e.Delta / 2));
            double offset = 0.0;
            if (e.Delta > 0)
            {
                sign = false;
                offset = Math.Max((double)0.0, (double)(scrollbar.VerticalOffset - num));
            }
            else
                offset = Math.Min(scrollbar.ScrollableHeight, scrollbar.VerticalOffset + num);
            if (offset != scrollbar.VerticalOffset)
            {
                scrollbar.ScrollToVerticalOffset(offset);
                e.Handled = true;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            var List = Paper_TestQuestionDAL. GetUMLQuestionList();
            foreach(var model in List)
            {
                QuestionList.Add(new UMLQuestionViewModel(model));
            }
            this.DataContext = this;
        }
      

        private void btnRef_Click(object sender, RoutedEventArgs e)
        {
            this.QuestionList.Clear();
            this.UserControl_Loaded(null, null);
        }

        private void Export_Click(object sender, RoutedEventArgs e)
        {
            string path = GetOpenFilesPath();
            string temppath = path;
            if (path == null)
            {
                return;
            }
            StepSequenceDiagramView view;
            var exlist = QuestionList.Where(x => x.IsChecked == true).ToList();
            try
            {
                foreach (var model in exlist)
                {
                    temppath += @"\" + model.TestQuestionID + "_" + model.QuestionName + ".png";
                    view = new StepSequenceDiagramView(model.TestQuestionID, model.QuestionName);
                    view.Window_Loaded(null, null);
                    view.ExportToPng(temppath);
                    temppath = path;
                }
                MessageBox.Show("图片已生成");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }
        private string GetOpenFilesPath()
        {
            string filePath = null;
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.Cancel)
                return filePath;
            filePath = dialog.SelectedPath.Trim();
            return filePath;
        }
    }

}
